home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13509 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  988 b 

  1. Path: ibm32.perftech.com!usenet
  2. From: murf@perftech.com (John Murphy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Date: 8 Apr 1996 12:47:03 GMT
  6. Organization: Performance Technology Inc
  7. Message-ID: <4kb1s7$6eu@ibm32.perftech.com>
  8. References: <4k6cjl$j8f@central.server.swt.edu>
  9. NNTP-Posting-Host: k5zba.perftech.com
  10. Mime-Version: 1.0
  11. NNTP-Posting-User: REVCO
  12. X-Newsreader: WinVN 0.93.11
  13.  
  14. In article <4k6cjl$j8f@central.server.swt.edu>, ln16674@nyssa.swt.edu 
  15. says...
  16. >
  17. >I have a challenge from a friend of mine.  He wanted me to reverse a string 
  18. >with recursion without using any additional variables or loops.  I got mine
  19. >to work by using exclusive or, but I needed an additional variable.  Can 
  20. >someone help with this problem without using the additional variable?
  21. >
  22. >Thanks
  23. You can swap two variables, x and y, with the following series of exclusive 
  24. or's:
  25.     x ^= y;
  26.     y ^= x;
  27.     x ^= y;
  28.  
  29. Or if you favor compactness over readability, you can use:
  30.  
  31.     x ^= y ^= x ^x y;
  32.  
  33. Murf
  34.  
  35.